<template>
  <div class="h-full w-full flex flex-col justify-between">
    <div class="form px-[15px] pt-[15px] border-bottom">
      <el-form>
        <el-row :gutter="12">
          <el-col :span="3">
            <el-form-item>
              <el-select class="!w-full" v-model="form.lottery_id">
                <el-option label="全部游戏" value="-1" />
                <el-option
                  v-for="item in gameTotal"
                  :key="item.LotteryId"
                  :label="item.Name"
                  :value="item.LotteryId"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item>
              <el-input v-model="form.issue" placeholder="请输入期号" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item>
              <div class="w-full flex justify-between">
                <el-date-picker
                  v-model="form.date"
                  type="daterange"
                  unlink-panels
                  range-separator="－"
                  start-placeholder="选择开始时间"
                  end-placeholder="选择结束时间"
                  :disabled-date="disableFutureDates"
                />
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <div class="flex justify-between">
              <el-form-item>
                <el-button type="primary" @click.stop="search">搜索</el-button>
                <el-button type="danger" @click.stop="emit('changeType', 'list')">返回</el-button>
              </el-form-item>
            </div>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="grow-[1] h-0 mx-[15px] mt-[10px]">
      <el-table
        scrollbar-always-on
        ref="elTableRef"
        v-loading="state.loading"
        :data="list"
        height="100%"
        border
        stripe
        fit
      >
        <template #empty>
          <ui-empty />
        </template>
        <el-table-column align="center" prop="LotteryName" label="游戏类型">
          <template #default="{ row }: TableItem">
            {{ row.LotteryName || "--" }}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="BitchNo" label="期号">
          <template #default="{ row }: TableItem">
            {{ row.BitchNo || "--" }}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="Cat3" label="下注内容">
          <template #default="{ row }: TableItem"> {{ row.Cat2 }}-{{ row.Cat3 }} </template>
        </el-table-column>
        <el-table-column align="center" prop="Amount" label="下注金额">
          <template #default="{ row }: TableItem">
            {{ row.Amount || "--" }}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="Msg" label="飞单结果">
          <template #default="{ row }: TableItem">
            {{ row.Msg || "--" }}
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="flex justify-end my-[10px] mx-[15px]">
      <ui-pagination
        :total="state.total"
        v-model:pageSize="state.pageSize"
        v-model:currentPage="state.page"
        @current-change="getData"
      ></ui-pagination>
    </div>
  </div>
</template>
<script setup lang="ts">
import moment from "moment";
import { useList } from "@/hooks/useList";
import { useGameStore } from "@/store/modules/game";
import { storeToRefs } from "pinia";
import { disableFutureDates, getDateArray } from "@/utils/common";
import { reqDapanReportFailDetail } from "@/api/flyalone";

interface TableItem {
  row: DapanReportDetailMo;
}
interface Params {
  Issue?: string;
  Name?: string;
  Type?: string;
  StartDate?: string;
  EndDate?: string;
  LotteryId?: string;
}
const emit = defineEmits(["changeType"]);
const gameStore = useGameStore();
const { gameTotal } = storeToRefs(gameStore);
const form = reactive({
  lottery_id: "-1",
  issue: "",
  date: getDateArray()["today"]
});

const getParams = () => {
  const params: Params = {};
  if (form.issue) {
    params.Issue = form.issue;
  }
  if (form.date) {
    params.StartDate = moment(form.date[0]).format("YYYY-MM-DD 00:00:00");
    params.EndDate = moment(form.date[1]).format("YYYY-MM-DD 23:59:59");
  }
  if (form.lottery_id && form.lottery_id != "-1") {
    params.LotteryId = form.lottery_id;
  }
  return params;
};

const { elTableRef, state, list, getData, search } = useList<DapanReportDetailMo, Params>(
  reqDapanReportFailDetail,
  getParams
);

defineExpose({
  getData
});
</script>
